package thread.threadLocal;

import java.lang.reflect.Constructor;

/**
 * @author huangguo
 * @time 19-8-3 下午2:07
 */
public class Main {
    ThreadLocal<Integer> t = new ThreadLocal<>();
    ThreadLocal<Integer> t1 = new ThreadLocal<>();
    ThreadLocal<Integer> t2 = new ThreadLocal<>();
    ThreadLocal<Integer> t3 = new ThreadLocal<>();
    ThreadLocal<Integer> t4 = new ThreadLocal<>();
    ThreadLocal<Integer> t5 = new ThreadLocal<>();
    ThreadLocal<Integer> t6 = new ThreadLocal<>();
    ThreadLocal<Integer> t7 = new ThreadLocal<>();
    ThreadLocal<Integer> t8 = new ThreadLocal<>();
    ThreadLocal<Integer> t11 = new ThreadLocal<>();
    ThreadLocal<Integer> t22 = new ThreadLocal<>();
    ThreadLocal<Integer> t33 = new ThreadLocal<>();
    ThreadLocal<Integer> t44 = new ThreadLocal<>();
    ThreadLocal<Integer> t55 = new ThreadLocal<>();
    ThreadLocal<Integer> t66 = new ThreadLocal<>();
    ThreadLocal<Integer> t77 = new ThreadLocal<>();
    ThreadLocal<Integer> t88 = new ThreadLocal<>();
    ThreadLocal<Integer> ttt = new ThreadLocal<>();
    ThreadLocal<Integer> tttt = new ThreadLocal<>();
    Integer tt;
    public static void main(String[] args) throws Exception {
        // 使用lambda的形式实例化线程任务类
        Main main = new Main();
        Runnable task1 = () -> {
            main.t.set(1);
            main.ttt.set(2);
            main.tttt.set(3);
            main.t1.set(11);
            main.t2.set(22);
            main.t3.set(33);
            main.t4.set(44);
            main.t5.set(55);
            main.t6.set(66);
            main.t7.set(77);
            main.t8.set(88);
            main.t11.set(111);
            main.t22.set(222);
            main.t33.set(333);
            main.t44.set(444);
            main.t55.set(555);
            main.t66.set(666);
            main.t77.set(777);
            main.tt = 1;
            while (true) {
                // 输出线程的名字
                try {
                    Thread.sleep(5000);
                    System.out.println("1:" + main.t.get());
                    main.t.get();
                    main.ttt.get();
                    main.tttt.get();
                    main.t1.get();
                    main.t2.get();
                    main.t3.get();
                    main.t4.get();
                    main.t5.get();
                    main.t6.get();
                    main.t7.get();
                    main.t8.get();
                    System.out.println("11:" + main.tt);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Runnable task2 = () -> {
            main.t.set(2);
            main.ttt.set(1);
            main.tttt.set(1);
            main.tt = 2;
            while (true) {
                // 输出线程的名字
                try {
                    Thread.sleep(5000);
                    System.out.println("2:" + main.t.get());
                    System.out.println("22:" + main.tt);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Runnable task3 = () -> {
            main.t.set(3);
            main.ttt.set(1);
            main.tttt.set(1);
            main.tt = 3;
            while (true) {
                // 输出线程的名字
                try {
                    Thread.sleep(5000);
                    System.out.println("3:" + main.t.get());
                    System.out.println("33:" + main.tt);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Runnable task4 = () -> {
            main.t.set(4);
            main.ttt.set(1);
            main.tttt.set(1);
            main.tt = 4;
            while (true) {
                // 输出线程的名字
                try {
                    Thread.sleep(5000);
                    System.out.println("4:" + main.t.get());
                    System.out.println("44:" + main.tt);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(task1).start();
        new Thread(task2).start();
        new Thread(task3).start();
        new Thread(task4).start();
    }
}

class Test{
    static int x;
    static {
        x = 1;
    }
    private Test(){}
}